package com.jinzhi.job.quartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.dao.ArchivesDao;
import com.jinzhi.jzweb.dao.ExaminationCostDao;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.DateUtil;
import com.jinzhi.sms.support.SmsManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author mengzhichao
 * @create 2022-11-30-9:02
 */
@Component // 把此类托管给 Spring，不能省略
public class TaskUtils {

    @Autowired
    private ExamRegistrationService examRegistrationService;

    @Autowired
    private ExaminationCostDao examinationCostDao;

    @Autowired
    private ArchivesDao archivesDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private SmsManager smsSerevice;

    @Autowired
    private IntegralUserOrderService integralUserOrderService;

    @Autowired
    private ZykResourcesSecretKeyService zykResourcesSecretKeyService;

    @Autowired
    private MessageplatfromService messageplatfromService;

    // 添加定时任务
//    @Scheduled(cron = "0/2 * * * * ?") // cron表达式：两秒执行一次
    @Scheduled(cron = "0 0 12 ? * WED") // cron表达式：表示每个星期三中午12点执行一次
    public void doTask(){
        List<Map<Object, Object>> list = examinationCostDao.selectSchoolName();
        if (list.size()>0){ //不为空
            List<ArchivesDO> insertList = new ArrayList<>();
            for (int i = 0;i<list.size();i++){
                ArchivesDO archivesDO = new ArchivesDO();
                archivesDO.setSchoolName(String.valueOf(list.get(i).get("schoolName")));
                archivesDO.setProvince(String.valueOf(list.get(i).get("province")));
                archivesDO.setUpdateTime(new Date());
                insertList.add(archivesDO);
            }
            for (int i =0;i<insertList.size();i++){
                archivesDao.insert(insertList.get(i));
                System.out.println("第"+(i+1)+"次执行新增定时任务！");
            }

        }

    }







    /**
     * 积分商城发短信功能定时任务
     * 0 0 10,14,16 * * ? 每天上午10点，下午2点，4点
     * 每天中午12点触发 ：0 0 12 * * ?
     * 0 0/5 * * * ?
     */
    @Scheduled(cron = "0 0 23 * * ?") // cron表达式：每天1点10分30秒触发任务
    public void integralDoTask(){
        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
        EntityWrapper<IntegralUserOrderDO> wrapper = new EntityWrapper();
        wrapper.ge("create_time",start);
        wrapper.lt("create_time",end);
        List<IntegralUserOrderDO> count = integralUserOrderService.selectList(wrapper);
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++积分商城发送短信："+count);
        if (count.size() > 0){//如果count不为空说明当天有数据则发送短信
            Map<String,String> map = new HashMap<>();
            map.put("type","jifennum");
            map.put("name","积分商城兑换");

            String system_invoice = dictService.selDicBy(map);
            Wrapper<MessageplatfromDO> wrapperor = new EntityWrapper<MessageplatfromDO>();
            wrapperor.eq("name","积分商城兑换");

            Map  map1org=new HashMap();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
            map1org.put("time",simpleDateFormat.format(new Date()));
            List<MessageplatfromDO> list=messageplatfromService.selectList(wrapperor);
            smsSerevice.send(system_invoice,list.get(0).getSmsDemo(),map1org);
        }
    }






//    @Scheduled(cron = "0/2 * * * * ?") // cron表达式：两秒执行一次
    @Scheduled(cron = "0 0 0 * * ?") // cron表达式：每天凌晨00:00:00点执行一次
    public void examRegistrationTask(){
        Wrapper<ExamRegistrationDO> wrapper = new EntityWrapper<ExamRegistrationDO>().orderBy("id", false);
        List<ExamRegistrationDO> examRegistrationDOS = examRegistrationService.selectList(wrapper);
        if(examRegistrationDOS.size() > 0){
            for (int i=0;i<examRegistrationDOS.size();i++){
                ExamRegistrationDO examRegistrationDO = new ExamRegistrationDO();
                examRegistrationDO.setId(examRegistrationDOS.get(i).getId());
                int result1 = new Date().compareTo(examRegistrationDOS.get(i).getRegistrationStartDate());
                int result2 = new Date().compareTo(examRegistrationDOS.get(i).getRegistrationEndDate());
                int result3 = new Date().compareTo(examRegistrationDOS.get(i).getTrainingEndDate());
                int count = examRegistrationDOS.get(i).getTotalTrainees() - examRegistrationDOS.get(i).getRegisteredTrainees();
                //未开始
                if(result1 < 0){
                    examRegistrationDO.setExamStatus(0);
                }
                //报名中
                if(result1 >=0 && result2 <=0){
                    examRegistrationDO.setExamStatus(1);

                }
                //已满员
                if(count <= 0){
                    examRegistrationDO.setExamStatus(2);
                }
                //已截止
                if(result2 > 0){
                    examRegistrationDO.setExamStatus(3);

                }
                //已结束
                if(result3 > 0){
                    examRegistrationDO.setExamStatus(4);
                }
                examRegistrationService.updateById(examRegistrationDO);
            }
        }
    }




    /**
     * 修改资源库秘钥过期状态
     */
    @Scheduled(cron = "0 0 * * * ?") // 每小时执行一次
    public void updateZykState(){
        Date now = new Date(); // 当前时间
        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(now, Locale.CHINA) + " 00:00:00"); // 当天开始时间
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(now, Locale.CHINA) + " 23:59:59"); // 当天结束时间

        // 查询当前小时内的过期秘钥
        EntityWrapper<ZykResourcesSecretKeyDO> wrapper = new EntityWrapper<>();
        wrapper.ge("ent_date", start); // 大于等于当天开始时间
        wrapper.lt("ent_date", end); // 小于当天结束时间
        wrapper.le("ent_date", now); // 小于等于当前时间

        List<ZykResourcesSecretKeyDO> list = zykResourcesSecretKeyService.selectList(wrapper);
        for (ZykResourcesSecretKeyDO item : list) {
            item.setUseState(0); // 将状态设置为过期
        }
        zykResourcesSecretKeyService.updateBatchById(list); // 批量更新
    }

}
